package net.yxsoft.kit;

import com.jfinal.kit.StrKit;
import net.yxsoft.bean.JsTreeNode;
import net.yxsoft.bean.JsTreeNodeFieldKey;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by mlb on 2016/5/16.
 * 树结构数据处理类
 */
public class JsTreeNodeKit {

  /*
  * 转化成树结构数据格式
  * */
  public static <T> List<JsTreeNode> SerializeJsTreeData(JsTreeNodeFieldKey fieldKey, List<T> models) throws NoSuchMethodException, SecurityException, InvocationTargetException, IllegalAccessException {

    List<JsTreeNode> _returnData = new ArrayList<JsTreeNode>();

    if(models == null || fieldKey == null || models.size() == 0) {
      return _returnData;
    }

    //有首节点
    if(StrKit.notBlank(fieldKey.getTopIdValue())) {

      JsTreeNode _tn = setTreeNodeFieldHasTop(fieldKey, models);
      if(_tn != null) {
        _returnData.add(_tn);
      }

    } else {
      //没有首节点，首节点是多个平行节点
      List<JsTreeNode> _tns = setTreeNodesFieldNoHasTop(fieldKey, models);

      if(_tns != null) {
        _returnData = _tns;
      }
    }

    return _returnData;
  }

  private static <T> JsTreeNode setTreeNodeFieldHasTop(JsTreeNodeFieldKey fieldKey, List<T> models) throws NoSuchMethodException, SecurityException, InvocationTargetException, IllegalAccessException {
    JsTreeNode _rtn = new JsTreeNode();
    JsTreeNode _topNode = setTopTreeNodeFieldHasTop(fieldKey, models);
    if(_topNode != null) {
      setChildrenTreeNodeField(fieldKey, models, _topNode);
    }

    return _topNode;

    //        Method mtn = class_tn.getMethod("setId",String.class);
    //        String _val =(String)method.invoke(model);
    //        mtn.invoke(_rtn,_val);
  }

  private static <T> JsTreeNode setTopTreeNodeFieldHasTop(JsTreeNodeFieldKey fieldKey, List<T> models) throws NoSuchMethodException, SecurityException, InvocationTargetException, IllegalAccessException {
    boolean _hasTop = false;
    JsTreeNode _rtn = new JsTreeNode();
    for(T model : models) {
      Method method_idval = model.getClass().getMethod("get" + fieldKey.getIdkey());
      String _idval = (String)method_idval.invoke(model);

      boolean _pass = false;

      if(_idval != null && _idval.equals(fieldKey.getTopIdValue())) {
        _pass = true;
      }


      if(_pass) {
        _hasTop = true;
        _rtn.setId(_idval);

        Method method_textval = model.getClass().getMethod("get" + fieldKey.getTextKey());
        String _textval = (String)method_textval.invoke(model);
        _rtn.setText(_textval);
      }

      if(_hasTop) {
        //models.remove(model);
        return _rtn;
      }
    }

    return null;

  }

  private static <T> List<JsTreeNode> setTreeNodesFieldNoHasTop(JsTreeNodeFieldKey fieldKey, List<T> models) throws NoSuchMethodException, SecurityException, InvocationTargetException, IllegalAccessException {

    List<JsTreeNode> _topNodes = setTopTreeNodesFieldNoHasTop(fieldKey, models);
    if(_topNodes != null) {
      for(JsTreeNode tn : _topNodes) {
        setChildrenTreeNodeField(fieldKey, models, tn);
      }
    }

    return _topNodes;

  }

  private static <T> List<JsTreeNode> setTopTreeNodesFieldNoHasTop(JsTreeNodeFieldKey fieldKey, List<T> models) throws NoSuchMethodException, SecurityException, InvocationTargetException, IllegalAccessException {
    List<JsTreeNode> _rtns = new ArrayList<JsTreeNode>();
    for(T model : models) {
      Method method_leafval = model.getClass().getMethod("get" + fieldKey.getLeafKey());
      String _leafval = String.valueOf(method_leafval.invoke(model));

      boolean _pass = false;

      if(_leafval != null && _leafval.equals(fieldKey.getLeafValue())) {
        _pass = true;
      }

      if(_pass) {

        JsTreeNode _tn = new JsTreeNode();

        Method method_idval = model.getClass().getMethod("get" + fieldKey.getIdkey());
        String _idval = String.valueOf(method_idval.invoke(model));
        _tn.setId(_idval);

        Method method_textval = model.getClass().getMethod("get" + fieldKey.getTextKey());
        String _textval = String.valueOf(method_textval.invoke(model));
        _tn.setText(_textval);

        _rtns.add(_tn);
      }
    }

    return _rtns;

  }

  private static <T> void setChildrenTreeNodeField(JsTreeNodeFieldKey fieldKey, List<T> models, JsTreeNode outmodel) throws NoSuchMethodException, SecurityException, InvocationTargetException, IllegalAccessException {

    if(models != null) {


      for(T model : models) {

        Method method_parentidval = model.getClass().getMethod("get" + fieldKey.getChildrenParentKey());
        String _parentidval = String.valueOf(method_parentidval.invoke(model));

        boolean _pass = false;

        if(_parentidval != null && _parentidval.equals(outmodel.getId())) {
          _pass = true;
        }

        if(_pass) {
          JsTreeNode _rtn = new JsTreeNode();

          Method method_idval = model.getClass().getMethod("get" + fieldKey.getIdkey());
          String _idval = String.valueOf(method_idval.invoke(model));
          _rtn.setId(_idval);

          Method method_textval = model.getClass().getMethod("get" + fieldKey.getTextKey());
          String _textval = (String)method_textval.invoke(model);
          _rtn.setText(_textval);
          //models.remove(model);

          List<JsTreeNode> _childs = outmodel.getChildren();
          if(_childs == null) {
            _childs = new ArrayList<JsTreeNode>();
            _childs.add(_rtn);
            outmodel.setChildren(_childs);
          } else {
            _childs.add(_rtn);
            outmodel.setChildren(_childs);
          }

          setChildrenTreeNodeField(fieldKey, models, _rtn);
        }
      }
    }

  }

}
